import pygame, sys
from pygame.locals import *
from datetime import datetime

#调速度
FPS = 30 # frames per second setting

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED   = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE  = (0, 0, 255)
PINK  = (255, 0, 255)
CardColor  = pygame.Color('Black')
GRAY  = pygame.Color('Dark Gray')

#窗口基础大小
WIN_WIDTH = 400
WIN_HEIGHT = 300

#窗口缩放倍数
SCALE = 3

#时间卡片宽度
TIME_CARD_WIDTH = 100 * SCALE
#时间卡片高度
TIME_CARD_HEIGHT = 200 * SCALE

SPLIT_WIDTH = 20 * SCALE

# 主体的左上角坐标
orgx = (WIN_WIDTH * SCALE - TIME_CARD_WIDTH * 3 - SPLIT_WIDTH * 2 ) / 2
orgy = (WIN_HEIGHT * SCALE - TIME_CARD_HEIGHT) / 2

# 坐标原点
center_x = WIN_WIDTH * SCALE / 2
center_y = WIN_HEIGHT * SCALE / 2

#控制动画的卡片位置偏移
HcardPos = 0
McardPos = 0
ScardPos = 0

def drawBg():
    ''' 填充背景 '''
    DISPLAYSURF.fill(pygame.Color("Gray"))
     #画分割 时 分 秒的点
    dotX = orgx + TIME_CARD_WIDTH + SPLIT_WIDTH / 2
    doxY = orgy + TIME_CARD_HEIGHT/2 
    #pygame.draw.circle(DISPLAYSURF, WHITE, (dotX, doxY - SPLIT_WIDTH), SPLIT_WIDTH/4)
    #pygame.draw.circle(DISPLAYSURF, WHITE, (dotX, doxY + SPLIT_WIDTH), SPLIT_WIDTH/4)

'''  画时间信息 '''
def drawInfo(tm):
    infoSurf = BASICFONT.render(tm.strftime("%A, %d. %B %Y %I:%M %p"), True, BLACK)
    infoRect = infoSurf.get_rect()
    infoRect.center = (center_x, 20 * SCALE)

    DISPLAYSURF.blit(infoSurf, infoRect)

def DrawFlipCard(timeNum, CardPosX, cardMovePos):
    newArect = pygame.Rect(CardPosX, orgy, TIME_CARD_WIDTH, TIME_CARD_HEIGHT)
    pygame.draw.rect(DISPLAYSURF, CardColor, newArect)
    
    ft = pygame.font.Font('freesansbold.ttf', 60 * SCALE)
    SecondSurf = ft.render("%02d"%(timeNum), True, WHITE)
    SecondRect = SecondSurf.get_rect()
    SecondRect.centerx = newArect.centerx
    SecondRect.centery = newArect.centery
    DISPLAYSURF.blit(SecondSurf, SecondRect)
    
    #画一条横线
    midLeft = (newArect.centerx - TIME_CARD_WIDTH/2, newArect.centery)
    midRight = (newArect.centerx + TIME_CARD_WIDTH/2, newArect.centery)

    pygame.draw.line(DISPLAYSURF, WHITE, midLeft, midRight, 2 * SCALE)

    if cardMovePos < TIME_CARD_HEIGHT:
        oldArect = pygame.Rect(CardPosX, orgy, TIME_CARD_WIDTH, TIME_CARD_HEIGHT)
        oldArect.move_ip(0, cardMovePos)
        pygame.draw.rect(DISPLAYSURF, CardColor, oldArect)
        
        oldSencond = (timeNum+59) % 60
        
        ft = pygame.font.Font('freesansbold.ttf', 40 * SCALE)
        SecondSurf = ft.render("%02d"%(oldSencond), True, WHITE)
        SecondRect = SecondSurf.get_rect()
        SecondRect.centerx = oldArect.centerx
        SecondRect.centery = oldArect.centery
        DISPLAYSURF.blit(SecondSurf, SecondRect)

        newArect = pygame.Rect(CardPosX, orgy - TIME_CARD_HEIGHT, TIME_CARD_WIDTH, TIME_CARD_HEIGHT)
        newArect.move_ip(0, cardMovePos)
        pygame.draw.rect(DISPLAYSURF, CardColor, newArect)
        
        ft = pygame.font.Font('freesansbold.ttf', 40 * SCALE)
        SecondSurf = ft.render("%02d"%(timeNum), True, WHITE)
        SecondRect = SecondSurf.get_rect()
        SecondRect.centerx = newArect.centerx
        SecondRect.centery = newArect.centery
        DISPLAYSURF.blit(SecondSurf, SecondRect)
    
    pygame.draw.line(DISPLAYSURF, WHITE, midLeft, midRight, 2 * SCALE)

    #盖住卡片移动时不想让用户看到的地方
    pygame.draw.rect(DISPLAYSURF, WHITE, (0,0, WIN_WIDTH * SCALE, orgy))
    pygame.draw.rect(DISPLAYSURF, WHITE, (0,orgy + TIME_CARD_HEIGHT, WIN_WIDTH * SCALE, WIN_HEIGHT * SCALE - TIME_CARD_HEIGHT - orgy))


'''  初始化配置 '''
def initEvn():
    global center_x, center_y
    center_x = WIN_WIDTH * SCALE / 2
    center_y = WIN_HEIGHT * SCALE / 2

def terminate():
    pygame.quit()
    sys.exit()

def main():
    global FPS, FPSCLOCK, DISPLAYSURF, BASICFONT, ScardPos, McardPos, HcardPos
    pygame.init()
    
    fpsClock = pygame.time.Clock()

    # set up the window
    DISPLAYSURF = pygame.display.set_mode((WIN_WIDTH * SCALE, WIN_HEIGHT * SCALE), 0, 32)
    pygame.display.set_caption('数字滚动时钟')

    BASICFONT = pygame.font.Font('freesansbold.ttf', 10 * SCALE)
      
    ''' 画背景 '''
    drawBg()

    Pause = False
    
    pygame.transform.scale2x(DISPLAYSURF)

    #获取当前时间
    tm = datetime.now()
    CurHour = tm.hour
    CurMinute = tm.minute
    CurSecond = tm.second

    ''' 主循环 '''
    while True: # the main game loop
        
        #获取当前时间
        tm = datetime.now()
        
        if ScardPos < TIME_CARD_HEIGHT:
            ScardPos += TIME_CARD_HEIGHT * 2 / FPS
        if McardPos < TIME_CARD_HEIGHT:
            McardPos += TIME_CARD_HEIGHT * 2 / FPS
        if HcardPos < TIME_CARD_HEIGHT:
            HcardPos += TIME_CARD_HEIGHT * 2 / FPS

        if not Pause:
            ''' 画背景 '''
            drawBg()
            # 时间变了，需要滚动动画
            if CurHour != tm.hour:
                HcardPos = 0
                CurHour = tm.hour
            DrawFlipCard(tm.hour, orgx, HcardPos)

            if CurMinute != tm.minute:
                McardPos = 0
                CurMinute = tm.minute
            DrawFlipCard(tm.minute, orgx + TIME_CARD_WIDTH + SPLIT_WIDTH, McardPos)

            if CurSecond != tm.second:
                ScardPos = 0
                CurSecond = tm.second
            DrawFlipCard(tm.second, orgx + TIME_CARD_WIDTH * 2 + SPLIT_WIDTH * 2, ScardPos)

            # 画信息
            drawInfo(tm)
        
        for event in pygame.event.get():
            # 键盘事件
            if (event.type == KEYDOWN):
                if event.key == K_SPACE:
                    Pause = not Pause
                elif event.key == K_ESCAPE:
                    terminate()
            elif event.type == QUIT:
                terminate()

        pygame.display.update()
        fpsClock.tick(FPS)

if __name__ == '__main__':
    main()